package com.ztjy.sys.controller;

import com.github.pagehelper.PageInfo;
import com.ztjy.annonation.MyLog;
import com.ztjy.bean.Dept;
import com.ztjy.bean.Module;
import com.ztjy.sys.biz.ModuleBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/sys/modules")
public class ModuleController {
    @Autowired
    private ModuleBiz mbiz;

    @ModelAttribute
    public void initModuleData(HttpSession session) {
        session.removeAttribute("mid");
        session.setAttribute("mid", 28);
    }

    @MyLog(value = "查询所有模块操作")  //这里添加了AOP的自定义注解
    @RequestMapping("/moduleList")
    public String moduleList(String mname, @RequestParam(value = "pid", defaultValue = "0") Integer pid, @RequestParam(defaultValue = "1", value = "pageno") Integer pageNo, Model model,HttpSession session) {
        Integer pageSize = 6;
        //分页
        PageInfo<Module> allModule = mbiz.findAllModule(mname, pid, pageNo, pageSize);
        model.addAttribute("mdInfo",allModule);

        //查询所有父模块 状态0 显示所有的
        List<Module> parentModule = mbiz.findAllParentModule(0);
        session.setAttribute("pallmodules", parentModule);

        model.addAttribute("mname", mname);
        model.addAttribute("pid", pid);
        return "/sys/modules/moduleList.html";
    }

    //添加模块
    @RequestMapping("/addModule")
    public String addModule(Module module){
        boolean flag = mbiz.addModule(module);
        if(flag){
            return "redirect:/sys/modules/moduleList";
        }else {
            return "forward:/sys/modules/moduleAdd.html";
        }
    }

    //跳到修改页面
        @RequestMapping("goUpdate/{moduleId}")
        public String goUpdate(@PathVariable("moduleId")Integer moduleId,Model model){
            Module module = mbiz.selectModuleById(moduleId);
            model.addAttribute("module",module);
            return "forward:/sys/modules/moduleUpdate.html";
        }

        //执行修改操作
        @RequestMapping("doUpdate")
        public String doUpdate(Module module){
        boolean flag = mbiz.update(module);
        if(flag){
            return "redirect:/sys/modules/moduleList";
        }else {
            return "forward:/sys/modules/moduleUpdate.html";
        }
    }


    //详情
    @RequestMapping("details/{moduleId}")
    public String details(@PathVariable("moduleId")Integer moduleId,Model model){
        Module module = mbiz.selectModuleById(moduleId);
        model.addAttribute("module",module);
        return "forward:/sys/modules/moduleView.html";
    }

    /**
     * 修改状态
     */
    @RequestMapping("/updatestate")
    public String updateState(Model model, Integer id, String chagestate){
        Module module = mbiz.selectModuleById(id);
        module.setModuleState(chagestate);
        mbiz.update(module);
        return "redirect:/sys/modules/moduleList";
    }

}
